package com.csxy.yuekaiyeye.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csxy.yuekaiyeye.R;
import com.csxy.yuekaiyeye.entity.EduTeacher;
import com.csxy.yuekaiyeye.entity.vo.TeacherQuery;
import com.csxy.yuekaiyeye.service.EduTeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "edu-teacher", description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {
    @Autowired
    private EduTeacherService eduTeacherService;
    @Operation(summary = "查询所有讲师")
    @GetMapping("findAll")
    public R getAllTeachers() {
        List<EduTeacher> list = eduTeacherService.list();
        return R.ok().data("items", list);
    }
    @Operation(summary = "逻辑删除讲师")
    @DeleteMapping("/deleteByid/{id}")
    public R deleteTeacherById(@PathVariable String id) {
        boolean result = eduTeacherService.removeById(id);
        if (!result) {
            return R.error();
        }
        return R.ok();
    }
    @Operation(summary = "分页查询讲师")
    @GetMapping("/pageList/{page}/{limit}")
    public R pageListTeacher(@PathVariable("page") long page,
                             @PathVariable("limit") long limit) {
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        Page<EduTeacher> pagelist = eduTeacherService.getPageList(pageParam, null);
        return R.ok().data("total", pagelist.getTotal()).data("rows", pagelist.getRecords());
    }
    @Operation(summary = "新增讲师",description = "新增讲师")
    @PostMapping("/save")
    public R save(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.save(eduTeacher);
        if (flag) {
            return R.ok();
        }else {
            return R.error();
        }
    }
    @Operation(summary = "根据id查询讲师",description = "根据id查询讲师")
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable String id) {
        EduTeacher eduTeacher = eduTeacherService.getById(id);
        return R.ok().data("item",eduTeacher);
    }
    @Operation(summary = "修改讲师",description = "修改讲师")
    @PostMapping("/updateById")
    public R updateById(@RequestBody EduTeacher eduTeacher) {
        boolean flag = eduTeacherService.updateById(eduTeacher);
        if (flag) {
            return R.ok();
            }else {
            return R.error();
        }
    }
    @Operation(summary = "条件查询带分页",description = "条件查询带分页")
    @PostMapping("/pageTeacherCondition/{page}/{limit}")
    public R pageListTeacherCondition(@Parameter(name = "page", description = "当前页码", required = true)
                                      @PathVariable("page") long page,
                                      @Parameter(name = "limit", description = "每页记录数", required = true)
                                      @PathVariable("limit") long limit,
                                      @RequestBody(required = false)TeacherQuery teacherQuery){
        Page<EduTeacher> pageParam = new Page<>(page, limit);
        eduTeacherService.pageQuery(pageParam,teacherQuery);
        List<EduTeacher> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }
}